package com.songshuai.sales.tool.service.impl;

import com.songshuai.sales.tool.dao.base.YixueBaseServiceTypeMapper;
import com.songshuai.sales.tool.dto.request.service.type.QueryServiceTypeReqDto;
import com.songshuai.sales.tool.dto.request.service.type.ServiceTypeReqDto;
import com.songshuai.sales.tool.dto.response.login.LoginUserResDto;
import com.songshuai.sales.tool.dto.response.service.type.ServiceTypeResDto;
import com.songshuai.sales.tool.model.base.YixueBaseServiceType;
import com.songshuai.sales.tool.service.YixueBaseServiceItemService;
import com.songshuai.sales.tool.service.YixueBaseServiceTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 此类为[基础代码]自动生成,已经拥有基本的增删改成操作 
 * <其他请自行扩展>
 * @Author: 刘汉轩
 * @Date: 2019-04-09 13:41:33
 * 
 */
@Slf4j
@Service
public class YixueBaseServiceTypeServiceImpl extends BaseServiceImpl implements YixueBaseServiceTypeService {
 
      
    @Autowired
	private YixueBaseServiceTypeMapper yixueBaseServiceTypeMapper;
    @Autowired
    private YixueBaseServiceItemService yixueBaseServiceItemService;
	
	
	/**
     * 新增
     * @param entity 
     */
    public int insertSelective (YixueBaseServiceType entity) {
        return this.yixueBaseServiceTypeMapper.insertSelective(entity);
    }	
    
    /**
     * 批量新增
     * @param list 
     */
    public void insertBatch(List<YixueBaseServiceType> list) {       
        this.yixueBaseServiceTypeMapper.insertBatch(list); 
    }
    
    /**
     * 根据主键修改单条数据
     * @param entity 
     */
    public int updateByPrimaryKey(YixueBaseServiceType entity) {
        return this.yixueBaseServiceTypeMapper.updateByPrimaryKey(entity);
    }
    
    /**
     * 根据主键批量修改数据
     * @param list 
     */
    public void updateBatch(List<YixueBaseServiceType> list) {      
        this.yixueBaseServiceTypeMapper.updateBatch(list);     
    }
    
    /**
     * 根据主键查询单条数据
     * @param id
     */
    public YixueBaseServiceType selectByPrimaryKey(Integer id) {
        return this.yixueBaseServiceTypeMapper.selectByPrimaryKey(id);
    }
    
    /**
     * 根据条件查询单条记录
     * @param entity 
     */
    public YixueBaseServiceType selectByEntity(YixueBaseServiceType entity) {
        return this.yixueBaseServiceTypeMapper.selectByEntity(entity);
    }
      
    /**
     * 根据条件查询多条记录
     * @param entity 
     */
    public List<YixueBaseServiceType> selectByEntityList(YixueBaseServiceType entity) {
        return this.yixueBaseServiceTypeMapper.selectByEntityList(entity);
    }
    
    /**
     * 根据条件查询Id
     * @param entity 
     */
    public Integer selectById(YixueBaseServiceType entity) {
        return this.yixueBaseServiceTypeMapper.selectById(entity);
    }
    
    /**
     * 根据条件查询Ids
     * @param entity 
     */
    public List<Integer> selectByIds(YixueBaseServiceType entity) {
        return this.yixueBaseServiceTypeMapper.selectByIds(entity);
    }
    
    /**
     * 根据主键删除单条记录
     * @param id
     */
    public void deleteByPrimaryKey(Integer id) {
        this.yixueBaseServiceTypeMapper.deleteByPrimaryKey(id);
    }
    
    /**
     * 根据主键删除多条记录
     * @param list
     */
    public void deleteBatch(List<Integer> list) {
        this.yixueBaseServiceTypeMapper.deleteBatch(list);
    }
    
    /**
     * 根据某些条件删除
     * @param entity
     */
    public void deleteByEntity(YixueBaseServiceType entity) {
        this.yixueBaseServiceTypeMapper.deleteByEntity(entity);
    }

    @Override
    public List<ServiceTypeResDto> queryServiceTypeList(QueryServiceTypeReqDto typeReqDto) {
        return yixueBaseServiceTypeMapper.selectServiceTypeList(typeReqDto.getTypeName(), typeReqDto.getOrderBy());
    }

    @Override
    public Integer saveOrUpdateServiceType(ServiceTypeReqDto type) {
        LoginUserResDto loginUser = getCurrentUser();
        Integer flag = 0;
        if(type.getId() != null){
            Map map = new HashMap();
            map.put("typeName", type.getTypeName());
            map.put("id", type.getId());
            Integer num = yixueBaseServiceTypeMapper.selectServiceTypeNumByName(map);
            if(num == 0){
                YixueBaseServiceType serviceType = new YixueBaseServiceType();
                BeanUtils.copyProperties(type, serviceType);
                serviceType.setUpdateBy(Integer.valueOf(loginUser.getUserId()));
                serviceType.setUpdateTime(new Date());
                yixueBaseServiceTypeMapper.updateByPrimaryKey(serviceType);
            }else{
                flag = 1;
            }
        }else{
            Map map = new HashMap();
            map.put("typeName", type.getTypeName());
            Integer num = yixueBaseServiceTypeMapper.selectServiceTypeNumByName(map);
            if(num == 0){
                YixueBaseServiceType serviceType = new YixueBaseServiceType();
                BeanUtils.copyProperties(type, serviceType);
                serviceType.setCreateTime(new Date());
                serviceType.setCreateBy(Integer.valueOf(loginUser.getUserId()));
                yixueBaseServiceTypeMapper.insertSelective(serviceType);
            }else{
                flag = 1;
            }
        }
        return flag;
    }

    @Override
    public Integer deleteServiceType(Integer id) {
        Integer flag = 0;
        YixueBaseServiceType serviceType = yixueBaseServiceTypeMapper.selectByPrimaryKey(id);
        if(null == serviceType){
            flag = 1;
        }else{
            Integer num = yixueBaseServiceItemService.queryServiceItemNumByTypeId(id);
            if(num == 0){
                LoginUserResDto loginUser = getCurrentUser();
                YixueBaseServiceType type = new YixueBaseServiceType();
                type.setIsdelete(1);
                type.setUpdateTime(new Date());
                type.setUpdateBy(Integer.valueOf(loginUser.getUserId()));
                type.setId(id);
                yixueBaseServiceTypeMapper.updateByPrimaryKey(type);
            }else{
                flag = 2;
            }
        }
        return flag;
    }
}